<?php

namespace app\api\model;

use app\admin\model\Express;
use app\exceptions\ApiException;
use ba\Exception;
use think\facade\Db;
use think\Model;

/**
 * Banner
 */
class VipOrder extends Model
{
    // 表名
    protected $name = 'vip_order';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;


    public function getVipCardOrderList($params)
    {
        $member_id = $params['member_id'];
        $status = $params['status'] ?? '';
        $map[] = ['member_id', '=', $member_id];
        if ($status) {
            $map[] = ['vo.status', '=', $status];
        } else {
            $map[] = ['vo.status', '>', 0];
        }

        $list = self::alias('vo')
            ->join('vip_card vc', 'vc.id = vo.vip_card_id')
            ->field('vo.id,vo.out_trade_no,vc.name,vc.image,vo.price,vo.status,waybill_token,vo.create_time,user_name')
            ->where($map)
            ->paginate(10)->toArray();

        if ($list['total'] == 0) {
            return api_message($list);
        }
        foreach ($list['data'] as &$val) {
            $val['image'] = full_url($val['image']);
            $val['create_time'] = date('Y-m-d H:i:s', $val['create_time']);
        }

        return api_message($list);
    }


    public function getVipCardOrderDetail($params)
    {
        $order_id = $params['order_id'] ?? 0;
        $info = self::alias('vo')
            ->join('vip_card vc', 'vc.id = vo.vip_card_id')
            ->field('vo.*,vo.out_trade_no,vc.name,vc.image,vo.status,waybill_token,user_name')
            ->where(['vo.id' => $order_id])
            ->find();
        if (!$info) {
            throw new ApiException('订单不存在');
        }
        $info['express_name'] = (new Express())->where(['delivery_id' => $info['express_name']])->value('delivery_name');
        $info['image'] = full_url($info['image']);
        $info['create_time'] = $info['create_time'] ? date('Y-m-d H:i:s', $info['create_time']) : '-';
        $info['pay_time'] = $info['pay_time'] ? date('Y-m-d H:i:s', $info['pay_time']) : '-';
        $info['send_time'] = $info['send_time'] ? date('Y-m-d H:i:s', $info['send_time']) : '-';
        $info['take_time'] = $info['take_time'] ? date('Y-m-d H:i:s', $info['take_time']) : '-';

        return api_message($info);
    }


    public function canelVipCard($params)
    {
        $order_id = $params['order_id'] ?? 0;
        $order = self::where(['id' => $order_id])->find();
        if (!$order) {
            throw new ApiException('订单不存在');
        }
        if ($order['status'] != 1) {
            throw new ApiException('该订单状态暂时不支持退款');
        }
        //执行退款
        try {
            Db::startTrans();
            self::where(['id' => $order_id])->update(['status' => 4, 'update_time' => time()]);
            (new Pay())->refund($order['transaction_id'], $order['out_trade_no'], $order['price'], $order['price']);
            Db::commit();
            return api_message([], '退款成功,请留意微信退款信息');
        } catch (Exception $e) {
            Db::rollback();
            throw new ApiException($e->getMessage());
        }
    }


    public function confirmVipCard($params)
    {
        $order_id = $params['order_id'] ?? 0;
        $order = self::where(['id' => $order_id])->find();
        if (!$order) {
            throw new ApiException('订单不存在');
        }
        if (!self::where(['id' => $order])->update(['status' => 3, 'update_time' => time(), 'take_time' => time()])) {
            throw new ApiException('操作失败');
        }
        return api_message([], '操作成功');
    }
}
